<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <!-- <template #toolbar>
        <a-button type="primary" @click="handleopen()" v-auth="'btn:add:cailiaoshouhuo'">
          <Icon icon="fluent:add-12-filled" /> {{ t('新增') }}
        </a-button>
      </template> -->
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.djno }}
        </a>
      </template>
      <template #projectno="{ record }">
        <!-- <a @click="handleDetailsForm(record)" >
          <span style="text-decoration: underline;margin-right: 5px;">查看</span>
        </a>
        {{ record.projectno }} -->
        <a @click="handleDetailsForm(record)" >
          {{ record.projectno }}
        </a>
      </template>


      <template #ddate="{ record }">
        {{ formatDate(record.ddate) }}
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <!-- <shform @register="registerDrawer" @success="handleSuccess" /> -->

    <!--:params="queryParams"  -->
    <ListSelect
      v-show="false"
      selectType="materialneedSelect"
      :checkbox="false"
      :selectList="listSelectRef"
      @select="handSelect"
      ref="materialRef"
    />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsMaterialReceivePmMaterialneedArrList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref, onMounted, watch } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  // import {
  //   pmMaterialneedArrDelete,
  //   pmMaterialneedArrListData,
  // } from '/@/api/material/receive/pmMaterialneedArr';
  import {  pmAsnShrecListAsnData } from '/@/api/material/receive/pmAsnShrec';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import shform from './shform.vue';
  import { ListSelect } from '/@/components/ListSelect';
  import { pmMaterialneedgetNeedDetail } from '/@/api/material/need/pmMaterialneed';
  import { useUserStore } from '/@/store/modules/user';
  const userStore = useUserStore();

  const { t } = useI18n('material.receive.pmMaterialneedArr');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    // value: router.currentRoute.value.meta.title || t('材料收货管理'),
    value: t('材料收货管理'),
  };

  const props = defineProps({
    projectno: { type: String, default: '' },
  });

  let listSelectRef = ref([]);

  let materialRef = ref<any>('');

  // 时间格式化日期，只保留年月日
  const formatDate = (date: string) => {
    if (!date) return '';
    const dateObj = new Date(date);
    const year = dateObj.getFullYear();
    const month = dateObj.getMonth() + 1;
    const day = dateObj.getDate();
    return `${year}-${month < 10 ? '0' + month : month}-${day < 10 ? '0' + day : day}`;
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('供货单号'),
        field: 'djno',
        component: 'Input',
      },
      {
        label: t('供应商'),
        field: 'venName',
        component: 'Input',
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
  {
      title: t('工程编号'),
      dataIndex: 'projectno',
      key: 'a.projectno',
      sorter: true,
      width: 130,
      align: 'left',
      slot: 'projectno',
      
    },
    {
      title: t('工程名称'),
      dataIndex: 'projectname',
      key: 'a.projectname',
      sorter: true,
      width: 200,
      align: 'left',
    },
    {
      title: t('供货单号'),
      dataIndex: 'djno',
      key: 'a.djno',
      sorter: true,
      width: 150,
      align: 'left',
      // slot: 'firstColumn',
    },
  
   
    {
      title: t('供应商'),
      dataIndex: 'venName',
      key: 'a.cvenName',
      sorter: true,
      width: 150,
      align: 'left',
    },
    {
      title: t('备注信息'),
      dataIndex: 'remarks',
      key: 'a.remarks',
      sorter: true,
      width: 150,
      align: 'left',
    },
    {
      title: t('供货日期'),
      dataIndex: 'ddate',
      key: 'a.ddate',
      sorter: true,
      width: 80,
      align: 'center',
      slot: 'ddate',
    },
   
    // {
    //   title: t('状态'),
    //   dataIndex: 'status',
    //   key: 'a.status',
    //   sorter: true,
    //   width: 130,
    //   align: 'center',
    //   dictType: 'sys_status',
    //   fixed: 'right',
    // },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        // icon: 'clarity:note-edit-line',
        label:t('确定收货'),
        title: t('确定收货'),
        onClick: handleForm.bind(this, { id: record.id }),
        auth: 'btn:edit:cailiaoshouhuo',
      },
      // {
      //   icon: 'ant-design:delete-outlined',
      //   color: 'error',
      //   title: t('删除材料收货'),
      //   popConfirm: {
      //     title: t('是否确认删除材料收货'),
      //     confirm: handleDelete.bind(this, { id: record.id }),
      //   },
      //   auth: 'btn:delete:cailiaoshouhuo',
      //   ifShow: () => record.status === '9',
      // },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm }] = useTable({
    api: pmAsnShrecListAsnData,
    beforeFetch: (params) => {
      params.projectno = router.currentRoute.value.query.projectno
      // params.projectname = router.currentRoute.value.query.projectname
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    immediate: true,
  });

  function handleDetailsForm(record: Recordable) {
    router.push({
      path: '/proj/info/pmProjectinformation/detailsForm',
      query: {
        id: record.projInfo.id,
        code: record.projectno,
        tabTitle: `项目详情(${record.projectno})`,
      },
    });
  }

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }
  function handleopen() {
    let listSelectRef = ref([]);
    // console.log(materialRef.value);

    materialRef.value.handleInputClick();
  }

  async function handleDelete(record: Recordable) {
    const res = await pmMaterialneedArrDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  function handleSuccess() {
    reload();
  }

  watch(
    () => props.projectno,
    () => {
      handleProjecte();
    },
  );

  // onMounted(async () => {
  //   handleProjecte();
  // });
  // async function handleProjecte() {
  //   await getForm().setFieldsValue({
  //     projectno: props.projectno?props.projectno:userStore.getProjecte.code,
  //   });
  //   reload();
  //   await getForm().setFieldsValue({
  //     projectno: props.projectno?props.projectno:userStore.getProjecte.code,
  //   });
  // }

  async function handSelect(selectData) {
    let res = await pmMaterialneedgetNeedDetail({ id: selectData[0].id });
    let pmMaterialneedArrsList = res.data.pmMaterialneedsList.map((item) => {
      item.sourceId = item.id;
      item.projectno = item.basInv ? item.basInv.pcode : '';
      item.isNewRecord = true;
      return item;
    });
    openDrawer(true, {
      projectno: res.data.projectno,
      name: res.data.name,
      needId: res.data.id,
      // isNewRecord:true,
      pmMaterialneedArrsList,
    });
    // let arr = await pmMaterialneedsTable.getDataSource();
    // selectData.forEach((item) => {
    //   let flag = true;
    //   arr.forEach((res) => {
    //     if (res.metercode == item.mcode) {
    //       flag = false;
    //     }
    //   });

    //   if (flag) {
    //     pmMaterialneedsTable.insertTableDataRecord({
    //       ...item,
    //       // id: new Date().getTime() + item.id,
    //       editable: true,
    //       isNewRecord: true,
    //       metercode: item.mcode,
    //       metername: item.mname,
    //       querycount: item.num,
    //     });
    //   }
    // });
  }
</script>
<style scoped>
  /deep/ .jeesite-basic-arrow{
    display: none;
  }
</style>
